package com.bos.controller;

import com.bos.controller.base.BaseController;
import com.bos.controller.base.imp.BaseControllerImp;
import com.bos.domain.Decidedzone;
import com.bos.domain.Staff;
import com.bos.domain.Subarea;
import com.bos.service.IDecidedZoneService;
import com.bos.service.IStaffService;
import com.bos.service.ISubareaService;
import com.bos.utils.BOSUtil;
import com.bos.utils.PageBean;
import com.crm.Customer;
import com.crm.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * controller方法的编写原则应该是如需调用service，那么就调用service中的一个方法即可，把需要事务控制的操作全部放到service层，以免
 * 出现问题，controller层只做简单处理和视图转发等功能
 */
@Controller
public class DecidedZoneController extends BaseControllerImp<Decidedzone> implements BaseController<Decidedzone> {

    @Autowired
    private IStaffService staffService;

    @Autowired
    private ISubareaService subareaService;

    @Autowired
    private IDecidedZoneService decidedZoneService;

    @Autowired
    CustomerService customerServiceProxy;

    private final String LIST = "page/base/decidedzone";

    @Autowired
    public DecidedZoneController(PageBean pageBean) {
        super(pageBean);
    }

    /**
     * 显示员工下拉菜单*/
    @RequestMapping(value = "/listStaff",method = RequestMethod.POST)
    @ResponseBody
    public String listStaff(){
        List<Staff> staffs = staffService.findAll();
        String json = java2json(staffs, new String[]{"telephone","haspda","deltag","station","standard","decidedzones"});
        return json;
    }

    /**
     * 显示未关联的分区*/
    @RequestMapping(value = "/associatedSubarea",method = RequestMethod.GET)
    @ResponseBody
    public String listNotAssociatedSubarea(){
        List<Subarea> subareas = subareaService.findByCriteria();
        String json = java2json(subareas, new String[]{"decidedzone","region","startnum","endnum","id"});
        return json;
    }

    /**
     * 添加定区*/
    @RequestMapping(value = "/addDecidedZone",method = RequestMethod.POST)
    public String addDecidedZone(Decidedzone decidedzone,String [] subareaId){
        decidedZoneService.save(decidedzone,subareaId);
        return "redirect:"+LIST;
    }

    /**定区分页查询
     * */
    @RequestMapping(value = "/decidedZonePageQuery",method = RequestMethod.GET)
    @ResponseBody
    public String pageQuery(int page ,int rows){
        //将当前页号和页面大小至于session域中，以便刷新时保持不变
        BOSUtil.getSession().setAttribute("decidedZonePage",page);
        BOSUtil.getSession().setAttribute("decidedZoneRows",rows);
        setPageAndRows(page,rows);
        staffService.pageQuery(super.pageBean);
        return java2json(new String[]{"currentPage","pageSize","detachedCriteria","subareas","decidedzones"});
    }

    @RequestMapping(value = "/findNotAssociatedCustomer",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String findNotAssociatedCustomer(){
        List<Customer> notAssociatedCustomer = customerServiceProxy.findNotAssociatedCustomer();
        return java2json(notAssociatedCustomer, new String[]{});
    }

    @RequestMapping(value = "/findAssociatedCustomer",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String findAssociatedCustomer(){
        List<Customer> notAssociatedCustomer = customerServiceProxy.findhasAssociatedCustomer();
        return java2json(notAssociatedCustomer, new String[]{});
    }

    @RequestMapping(value = "/AssociatedCustomer",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public String AssociatedCustomer(String id,int [] customerIds){
        List<Integer> list = new ArrayList<>();
        if (customerIds != null) {
            for (int customerId : customerIds) {
                list.add(customerId);
            }
            customerServiceProxy.update(id,list);
        }else {
            list.add(-1);
            customerServiceProxy.update(id,list);
        }
        return  "base/decidedzone";
    }

    /**查询定区关联的分区
     **/

    @RequestMapping(value = "/ListAssociatedSubareas",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String AssociatedSubareas(String id){
        Decidedzone decidedzone = decidedZoneService.findById(id);
        return java2json(decidedzone.getSubareas(),new String[]{"decidedzone","subareas"});
    }

    @RequestMapping(value = "/ListAssociatedCustomers",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String AssociatedCustomers(String id){
        List<Customer> customers = customerServiceProxy.findByDecidedZoneId(id);
        return java2json(customers,new String[]{"decidedZoneId",});
    }

}
